<?php
namespace Admin\Controller;

class PermitController extends BaseController{
	public function index(){
		$Permit = D('Permit');
		$object = $Permit->all($this->page($Permit->total()));
		if ($object){
			foreach ($object as $key=>$value){
				$object[$key]['obj'] = $Permit->all3($value['id']);
			}
		}
		$this->assign('All',$object);
		$this->display();
	}
	
	public function sort(){
		if (IS_POST){
			$Permit = D('Permit');
			foreach (I('post.sort') as $key=>$value){
				if (!is_numeric($value)) continue;
				$Permit->sort($key,$value);
			}
			$this->success(C('PREV_URL'),'权限排序成功！');
		}
	}
	
	public function selected(){
		if (I('get.id')){
			$Permit = D('Permit');
			$object = $Permit->one();
			if (!$object) $this->failed('不存在此权限！');
			if ($object['selected'] == 0){
				if (!$Permit->selected(1)) $this->failed('设置默认权限失败！');
			}else{
				if (!$Permit->selected(0)) $this->failed('取消默认权限失败！');
			}
			$this->success(C('PREV_URL'));
		}else{
			$this->failed('非法操作！');
		}
	}
	
	public function output(){
		$Permit = D('Permit');
		$object = $Permit->all2();
		$output = '<?php return array(';
		if ($object){
			$output .= "'PERMIT'=>array(";
			foreach ($object as $value){
				$output .= "'".$value['c']."'=>array(";
				$output .= "'".$value['a']."'=>".$value['id'];
				$object2 = $Permit->all3($value['id']);
				if ($object2){
					foreach ($object2 as $v){
						$output .= ",'".$v['a']."'=>".$v['id'];
					}
				}
				$output .= '),';
			}
			$output = substr($output,0,-1);
			$output .= ')';
		}
		$output .= ');?>';
		file_put_contents(ROOT_PATH.'/App/Admin/Conf/permit.php',$output) ? $this->success(U(CONTROLLER_NAME.'/index'),'配置文件更新成功！') : $this->failed('配置文件更新失败！');
	}
}